package com.luxoft.scoremanager.server.dao.listimpl;

import com.luxoft.scoremanager.server.dao.ScorePackageDao;
import com.luxoft.scoremanager.server.model.entity.ScorePackage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

public class ListScorePackageDao implements ScorePackageDao {

    private List<ScorePackage> scorePackageList = new ArrayList<ScorePackage>();

    public ListScorePackageDao() {
        init();
    }

    private void init() {
        scorePackageList.add(new ScorePackage(100, "IN_COMMITTEE"));
        scorePackageList.add(new ScorePackage(123, "PRE_COMMITTEE"));
    }

    @Override
    public void addPackage(ScorePackage scorePackage) {
        checkNotNull(scorePackage);
        if (scorePackage.getId() == null) {
            scorePackage.setId((int) new Date().getTime());
        }
        scorePackageList.add(scorePackage);
    }

    @Override
    public void updatePackage(ScorePackage scorePackage) {
        checkNotNull(scorePackage);
        if (scorePackage.getId() == null) {
            scorePackage.setId((int) new Date().getTime());
            scorePackageList.add(scorePackage);
        } else {
            scorePackageList.remove(scorePackage);
            scorePackageList.add(scorePackage);
        }
    }

    @Override
    public void deletePackage(ScorePackage score) {
        checkNotNull(score);
        checkNotNull(score.getId());
        scorePackageList.remove(score);
    }

    @Override
    public List<ScorePackage> getAll() {
        return scorePackageList;
    }

    @Override
    public ScorePackage getById(int id) {
        checkNotNull(id);
        for (ScorePackage scorePackage : scorePackageList) {
            if (id == scorePackage.getId()) {
                return scorePackage;
            }
        }
        return null;
    }
}
